/**
 * @Author : 2037630@163.com
 * @Timestamp : 2017-04-18
 */
var Controller = require("core/controller");
var net = require('util/net')
var log = require("util/log")
var constant = require('config/constant')
var storage = require('util/storage')
var navigate = require('util/navigate')
var server = require('config/server')
var router = require('config/router')
var user = require('module/userModule')
var notify = sm('do_Notification')
var page=sm("do_Page")
Controller({
	name : 'login',
	mapping : {
		"tf_password.text" : "password",
		"realName.text" : "realName"
	},
	data : {
		userPhoto : '',
		userName : '',
		realName : '',
		password : ''
	},
	ui:{
		userImage:"userImage",
		realName:"realName",
		userImage:"userImage"
	},
	event : {
		'btn_login' : {
			'touch' : function() {
				this.login()
			}
		},
		'btn_other' : {
			'touch' : function() {
//				user.clearUserData()
				navigate.redirectTo({
					view:router.app.login
				})
				
			}
		}
	},
	rules : {
	// TODO: 添加自定义数据验证
	},
	methods : {
		login : function() {
			log.info(this.ui)
			net.send({
				server : server.organization.login,
				data : {
					username : this.data.userName,
					password : this.data.password
				},
				success : function(data) {
					// 存储用户信息
					user.updateUserData(data)
					// TODO:添加临时存储
					navigate.redirectTo({
						view : router.index
					})
				},
				error : function(data) {
					notify.toast(data.reason)
				}
			})
		}
	},
	loaded : function() {
		this.data.userPhoto = user.data.userPhoto
		this.data.userName = user.data.userName
		this.data.realName = user.data.realName
		this.ui.userImage.source=this.data.userPhoto
		this.ui.realName.text=this.data.realName
		if(!user.data.userPhoto){
			this.ui.userImage.source="source://image/app/logo.png";
		}
		else{
		log.info("999999")
			this.ui.userImage.source=user.data.userPhoto
			log.info(user.data.userPhoto)
			log.info(this.ui.userImage.source)
			this.ui.userImage.redraw()
			
		}

	}
})
